package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * 功能：声网语音/视频自定义消息
 */
@MessageTag(value = IMMessageType.TYPE_RXAGORACALL, flag = MessageTag.ISPERSISTED | MessageTag.ISCOUNTED)
public class RxAgoraCallMessageContent extends MessageContent {
    private String sendContent; //发送方消息展示
    private String receiveContent; //接收方消息展示
    private String type; //通话类型（语音、视频）
    private String senderId; //发送方id
    private String targetId;


    public static RxAgoraCallMessageContent setRxAgoraCallMessageContent(String sendContent, String receiveContent, String type, String senderId, String targetId) {
        RxAgoraCallMessageContent mRXMessageContent = new RxAgoraCallMessageContent();
        mRXMessageContent.sendContent = sendContent;
        mRXMessageContent.receiveContent = receiveContent;
        mRXMessageContent.type = type;
        mRXMessageContent.senderId = senderId;
        mRXMessageContent.targetId = targetId;
        return mRXMessageContent;
    }

    @Override
    public byte[] encode() {
        JSONObject object = new JSONObject();
        object.put("sendContent", sendContent);
        object.put("receiveContent", receiveContent);
        object.put("senderId", senderId);
        object.put("targetId", targetId);
        object.put("type", type);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RxAgoraCallMessageContent() {
    }

    public RxAgoraCallMessageContent(byte[] data) {
        super(data);
        String jsonStr = null;
        try {
            jsonStr = new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);

            setReceiveContent(object.getString("receiveContent"));
            setSendContent(object.getString("sendContent"));
            setSenderId(object.getString("senderId"));
            setTargetId(object.getString("targetId"));
            setType(object.getString("type"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, sendContent);
        ParcelUtils.writeToParcel(dest, receiveContent);
        ParcelUtils.writeToParcel(dest, senderId);
        ParcelUtils.writeToParcel(dest, targetId);
        ParcelUtils.writeToParcel(dest, type);
    }

    public static final Creator<RxAgoraCallMessageContent> CREATOR = new Creator<RxAgoraCallMessageContent>() {

        @Override
        public RxAgoraCallMessageContent createFromParcel(Parcel source) {
            return new RxAgoraCallMessageContent(source);
        }

        @Override
        public RxAgoraCallMessageContent[] newArray(int size) {
            return new RxAgoraCallMessageContent[size];
        }
    };


    public RxAgoraCallMessageContent(Parcel parcel) {
        sendContent = ParcelUtils.readFromParcel(parcel);
        receiveContent = ParcelUtils.readFromParcel(parcel);
        senderId = ParcelUtils.readFromParcel(parcel);
        targetId = ParcelUtils.readFromParcel(parcel);
        type = ParcelUtils.readFromParcel(parcel);
    }


    @Override
    public int describeContents() {
        return 0;
    }


    public String getSendContent() {
        return sendContent;
    }

    public void setSendContent(String sendContent) {
        this.sendContent = sendContent;
    }

    public String getReceiveContent() {
        return receiveContent;
    }

    public void setReceiveContent(String receiveContent) {
        this.receiveContent = receiveContent;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSenderId() {
        return senderId;
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }

    public String getTargetId() {
        return targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }
}
